package com.yanggeng.demo.beginnerguidedemo.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.yanggeng.demo.beginnerguidedemo.utils.DeviceParams;
import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.utils.ToastUtil;
import com.yanzhenjie.album.Action;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumConfig;
import com.yanzhenjie.album.AlbumFile;
import com.yanzhenjie.album.AlbumLoader;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 图片描点
 *
 * @author aiden@tronsis.com
 * @date 2018/5/31 14:42
 */
public class ImagePointActivity extends BaseActivity {

    @BindView(R.id.btn_select_image)
    Button btnSelectImage;
    @BindView(R.id.iv_old)
    ImageView ivOld;
    @BindView(R.id.et_left_right)
    EditText etLeftRight;
    @BindView(R.id.et_top_bottom)
    EditText etTopBottom;
    @BindView(R.id.btn_point)
    Button btnPoint;
    @BindView(R.id.iv_new)
    ImageView ivNew;

    private Context mContext;
    private String mImagePath;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_point);
        ButterKnife.bind(this);
        setTitle(true, "图片描点");
        mContext = this;
        Album.initialize(AlbumConfig.newBuilder(this)
                .setAlbumLoader(new AlbumLoader() {
                    @Override
                    public void load(ImageView imageView, AlbumFile albumFile) {
                        load(imageView, albumFile.getPath());
                    }

                    @Override
                    public void load(ImageView imageView, String url) {
                        Glide.with(imageView.getContext())
                                .load(url)
                                .into(imageView);
                    }
                }).build());
    }

    @OnClick({R.id.btn_select_image, R.id.btn_point})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_select_image:
                selectImage();
                break;
            case R.id.btn_point:
                imagePoint(50, 100, 100, 20, 200, 50, 30, 20, 20, 50);
                break;
        }
    }

    private void selectImage() {
        Album.image(this)
                .singleChoice()
                .camera(true)
                .onResult(new Action<ArrayList<AlbumFile>>() {
                    @Override
                    public void onAction(@NonNull ArrayList<AlbumFile> result) {
                        if (result.size() > 0) {
                            mImagePath = result.get(0).getPath();
                            Glide.with(mContext).load(mImagePath).into(ivOld);
                        }
                    }
                })
                .onCancel(new Action<String>() {
                    @Override
                    public void onAction(@NonNull String result) {
                        Log.e("ImagePointActivity", result);
                    }
                })
                .start();
    }

    private void imagePoint(float x, float y, float xMax, float xMin, float yMax, float yMin, float left, float right, float top, float bottom) {
        if (TextUtils.isEmpty(mImagePath)) {
            ToastUtil.centerToast(mContext, "请选择图片");
            return;
        }
//        String leftRightPadding = etLeftRight.getText().toString().trim();
//        if (TextUtils.isEmpty(leftRightPadding)) {
//            ToastUtil.centerToast(mContext, "请输入左右边距");
//            return;
//        }
//        String topBottomPadding = etTopBottom.getText().toString().trim();
//        if (TextUtils.isEmpty(topBottomPadding)) {
//            ToastUtil.centerToast(mContext, "请输入上下边距");
//            return;
//        }
//        float leftRight = Float.valueOf(leftRightPadding);
//        float topBottom = Float.valueOf(topBottomPadding);
        ivOld.destroyDrawingCache();
        ivOld.setDrawingCacheEnabled(true);
        ivOld.buildDrawingCache();
        Bitmap bitmap = ivOld.getDrawingCache();

        Bitmap old = BitmapFactory.decodeFile(mImagePath).copy(Bitmap.Config.ARGB_8888, true);

        float scale = bitmap.getWidth() / (float) old.getWidth();

        float leftPadding = left * scale;
        float rightPadding = right * scale;
        float topPadding = top * scale;
        float bottomPadding = bottom * scale;

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        float tableWidth = bitmap.getWidth() - leftPadding - rightPadding;
        float tableHeight = bitmap.getHeight() - topPadding - bottomPadding;

        float xCoordinate = leftPadding + (x - xMin) / (xMax - xMin) * tableWidth;
        float yCoordinate = topPadding + ( 1 - (y - yMin) / (yMax - yMin)) * tableHeight;
//        canvas.drawCircle(xCoordinate, yCoordinate, 10, paint);

        float lineWidth = DeviceParams.dip2px(mContext, 1);

        Paint linePaint = new Paint();
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setColor(Color.RED);
        linePaint.setStrokeWidth(lineWidth);

        canvas.drawLine(xCoordinate - 10, yCoordinate - lineWidth / 3, xCoordinate + 10, yCoordinate - lineWidth / 3, linePaint);
        canvas.drawLine(xCoordinate, yCoordinate - 10, xCoordinate, yCoordinate + 10, linePaint);

//        paint.setStrokeWidth(DeviceParams.dip2px(this, 2));
//        paint.setColor(Color.GRAY);
//        canvas.drawLine(leftPadding, 0, leftPadding, bitmap.getHeight(), paint);
//        canvas.drawLine(0, topPadding, bitmap.getWidth(), topPadding, paint);
//        canvas.drawLine(leftPadding + tableWidth, 0, leftPadding + tableWidth, bitmap.getHeight(), paint);
//        canvas.drawLine(0, topPadding + tableHeight, bitmap.getWidth(), topPadding + tableHeight, paint);

//        Random random = new Random();
//        for (int i = 0; i < 5; i++) {
//            float randomHeight = random.nextFloat();
//        canvas.drawCircle(leftPadding + tableWidth / 4 * i, randomHeight * tableHeight + topPadding, 10, paint);
//        }
        ivNew.setImageBitmap(bitmap);
    }
}
